package com.haiertvbic.tvnow.adapter;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.haiertvbic.tvnow.R;
import com.haiertvbic.tvnow.common.Constant;
import com.haiertvbic.tvnow.holder.ScreenshortHolder;
import com.haiertvbic.tvnow.model.ProgramBean;
import com.haiertvbic.tvnow.network.EPGWeb;
import com.haiertvbic.tvnow.util.DensityUtil;
import com.haiertvbic.tvnow.util.SLog;

public class ProAdapater extends BaseAdapter {
	private static final String TAG = null;
	private ArrayList<ProgramBean> pro_list;
	private Context context;
	private int sreen_height;
	private Map<String, SoftReference<Bitmap>> pro_picCaches = new HashMap<String, SoftReference<Bitmap>>();

	public ProAdapater(ArrayList<ProgramBean> pro_list, int sreen_height,
			Context applicationContext) {
		// TODO Auto-generated constructor stub
		this.pro_list = pro_list;
		this.context = applicationContext;
		this.sreen_height = (sreen_height)
				/ Constant.CHANNEL_NUMBER;//其实现在是整个屏幕的宽度
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		SLog.d(TAG, "==========getCount====================");
		return pro_list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return pro_list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View convertView, ViewGroup arg2) {
		SLog.d(TAG, "==========getView====================");
		ScreenshortHolder holder = new ScreenshortHolder();
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.pro_detail_item, null);
			// =========动态控制图片张数=================
			// holder.sc_chan_item_ll = (RelativeLayout) convertView
			// .findViewById(R.id.detail_item_ll);
			// android.view.ViewGroup.LayoutParams ll_lp =
			// holder.sc_chan_item_ll
			// .getLayoutParams();
			// ll_lp.height = (sreen_height - Constant.RANGE_TOP)
			// / Constant.CHANNEL_NUMBER;
			// holder.sc_chan_item_ll.setLayoutParams(ll_lp);
			// =========动态控制图片张数=================
			holder.pip_iv = (ImageView) convertView
					.findViewById(R.id.detail_iv);
			// 字的背景颜色
			// holder.screenshort_background_ll = (LinearLayout) convertView
			// .findViewById(R.id.pro_detail_background_ll);
			holder.pro_tv = (TextView) convertView
					.findViewById(R.id.detail_pro_tv);
			holder.pro_stime_tv = (TextView) convertView
					.findViewById(R.id.detail_stime_tv);
			// holder.toNext = (TextView) convertView.findViewById(R.id.toNext);
			convertView.setTag(holder);
		} else {
			holder = (ScreenshortHolder) convertView.getTag();
		}
		ProgramBean p = pro_list.get(arg0);
		if (p != null) {
			// urlStr =
			// "http://images-cdn.digu.com/19677c6591944ab2a47f5f8c88fc59660005.jpg";
			if (p.getPicurl()!=null) {
				String urlStr = p.getPicurl();
//				urlStr = "";
				if (pro_picCaches.containsKey(urlStr)
						&& (pro_picCaches.get(urlStr).get() != null)) {
					// 若软引用中有，给image设置
					holder.pip_iv.setImageBitmap(pro_picCaches.get(urlStr).get());
					holder.pip_iv.setVisibility(View.VISIBLE);
					SLog.d(TAG, "软引用的节目详情");
				} else {
					// 开异步线程去台标
					SLog.d(TAG, "开异步线程取的节目详情,用默认的");
					// holder.pip_iv.setImageResource(R.drawable.tv_now);
					new PipAsyncTask(holder.pip_iv).execute(urlStr);
				}
			}
			
			// 对字体设置===================================
			// holder.screenshort_background_ll.setVisibility(View.VISIBLE);
			holder.pro_tv.setText("    "+p.getPn());
			SLog.d(TAG, "节目名字==getView====" + p.getPn());
			holder.pro_stime_tv.setText("    "+p.getPstime());
			SLog.d(TAG, "节目的开始时间==getView=========" + p.getPstime());
			// Constant.PRO_TYPE = 92 + "";
			// if (p.getPtype().contains(Constant.PRO_TYPE)) {
			// holder.toNext.setVisibility(View.VISIBLE);
			// } else {
			// holder.toNext.setVisibility(View.GONE);
			// }
		}
		// if(arg0==0)
		// {
		// convertView.setFocusable(true);
		// convertView.setFocusableInTouchMode(true);
		// convertView.requestFocus();
		// }
		return convertView;

	}

	class PipAsyncTask extends AsyncTask<String, Void, Bitmap> {
		private ImageView pip_iv;

		public PipAsyncTask(ImageView pip_iv) {
			// TODO Auto-generated constructor stub
			this.pip_iv = pip_iv;
		}

		@Override
		protected Bitmap doInBackground(String... params) {
			String urlStr = params[0];
			Bitmap bm = null;
			SLog.d(TAG, "取得节目信息图片的url==="+urlStr);
			bm = EPGWeb.getBitmapFromURL(context, urlStr);
			// 获得Bitmap的高和宽
			if (bm != null) {
				int bmpWidth = bm.getWidth();
				int bmpHeight = bm.getHeight();
				//如果屏幕的宽度1/5，小于图片的宽度在变更
				if (bmpWidth > sreen_height) {
					//产生resize后的Bitmap对象 
					float  scale = sreen_height/bmpWidth;
					Bitmap resizeBmp = null;
					if (DensityUtil.dip2px(context, sreen_height)>0&&DensityUtil.dip2px(context, bmpHeight*scale)>0) {
						resizeBmp = Bitmap.createScaledBitmap(bm, DensityUtil.dip2px(context, sreen_height), DensityUtil.dip2px(context, bmpHeight*scale), true);
					}
					pro_picCaches.put(urlStr, new SoftReference<Bitmap>(resizeBmp));
					bm = resizeBmp;
					SLog.d(TAG, "算出后的图片宽是===="+sreen_height);
				}
			}
			SLog.d(TAG, "====doInBackground============获取到节目详情图片了");
			return bm;
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result != null) {
				pip_iv.setImageBitmap(result);
				pip_iv.setVisibility(View.VISIBLE);
			}
		}
	}
}
